﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Control;
using Ch.Admin.Bfs.Sms.Sdmx.Utils;
using System.Drawing;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration
{
    /// <summary>
    /// Class holding specific configuration to use for codebook generation
    /// </summary>
    [Serializable]
    public class BookConfiguration
    {
        #region Constructor

        public BookConfiguration()
        {
            Margins = new BookMargins();
         //   internationalLogo = new InternationalizedLogo();

            ShowCodelist = ShowCodelistResume = true;
            AnnotIgnoreList = new List<string>();
            AnnotReplaceType = new SerializableDictionary<string, InternationalizedString>();

            CoverTitle = new InternationalizedString();
            CoverTitle.Add("en", "Please define cover title");
            CoverTitle.Add("de", "Bitte legen Sie den Titel des Covers");
            CoverTitle.Add("fr", "Merci d'indiquer le titre du codebook");
            CoverTitle.Add("it", "Si prega di includere il titolo del libro");

            CoverSubTitle = new InternationalizedString();
            CoverSubTitle.Add("en", "Please define survey domain for Cover");
            CoverSubTitle.Add("de", "Definieren Sie bitte Umfrage Domäne");
            CoverSubTitle.Add("fr", "Merci d'indiquer le domaine d'enquête");
            CoverSubTitle.Add("it", "Per favore definire il dominio di indagine");

            TitleModuleCodelist = new InternationalizedString();
            TitleModuleCodelist.Add("en", "Lists of codes used");
            TitleModuleCodelist.Add("de", "Listen der verwendeten codes");
            TitleModuleCodelist.Add("fr", "Listes de codes utilisés");
            TitleModuleCodelist.Add("it", "Elenchi dei codici utilizzati");

            TableOfContentName = new InternationalizedString();
            TableOfContentName.Add("en", "Table of content");
            TableOfContentName.Add("de", "Inhaltsverzeichnis");
            TableOfContentName.Add("fr", "Table des matières");
            TableOfContentName.Add("it", "Sommario");

            TitleVarDetail = new InternationalizedString();
            TitleVarDetail.Add("en", "Detail of the variables of the module");
            TitleVarDetail.Add("de", "Detail der Variablen des Moduls");
            TitleVarDetail.Add("fr", "Détail des variables du module");
            TitleVarDetail.Add("it", "Dettaglio delle variabili del modulo");

            AddParagraph = true;
        }
        #endregion

        #region Properties

        //[Category("Cover Logo")]
        //[Description("Bitmap for top cover image")]
        //[Browsable(true)]
        //InternationalizedLogo internationalLogo { get; set; }

        #region LOGOS

        [XmlIgnore]
        [DescriptionAttribute("Defines the image of the english version of cover logo")]
        [DisplayName("English Cover Logo")]
        [Category("Logo")]
        public Bitmap LogoEN { get; set; }

        [Browsable(false)]
        public byte[] MyImageBytesEN
        {
            get
            {
                ImageConverter converter = new ImageConverter();
                return (byte[])converter.ConvertTo(LogoEN, typeof(byte[]));
            }
            set
            {
                LogoEN = value.Length > 0 ? new Bitmap(new System.IO.MemoryStream(value)) : null;
            }
        }
        [XmlIgnore]
        [DescriptionAttribute("Defines the image of the german version of cover logo")]
        [DisplayName("German Cover Logo")]
        [Category("Logo")]
        public Bitmap LogoDE { get; set; }

        [Browsable(false)]
        public byte[] MyImageBytesDE
        {
            get
            {
                ImageConverter converter = new ImageConverter();
                return (byte[])converter.ConvertTo(LogoDE, typeof(byte[]));
            }
            set
            {
                LogoDE = value.Length > 0 ? new Bitmap(new System.IO.MemoryStream(value)) : null;
            }
        }
        [XmlIgnore]
        [DescriptionAttribute("Defines the image of the french version of cover logo")]
        [DisplayName("French Cover Logo")]
        [Category("Logo")]
        public Bitmap LogoFR { get; set; }

        [Browsable(false)]
        public byte[] MyImageBytesFR
        {
            get
            {
                ImageConverter converter = new ImageConverter();
                return (byte[])converter.ConvertTo(LogoFR, typeof(byte[]));
            }
            set
            {
                LogoFR = new Bitmap(new System.IO.MemoryStream(value));
            }
        }

        [XmlIgnore]
        [DescriptionAttribute("Defines the image of the italian version of cover logo")]
        [DisplayName("Italian Cover Logo")]
        [Category("Logo")]
        public Bitmap LogoIT { get; set; }

        [Browsable(false)]
        public byte[] MyImageBytesIT
        {
            get
            {
                ImageConverter converter = new ImageConverter();
                return (byte[])converter.ConvertTo(LogoIT, typeof(byte[]));
            }
            set
            {
                LogoIT = value.Length > 0 ? new Bitmap(new System.IO.MemoryStream(value)) : null;
            }
        }
        #endregion

        [XmlIgnore]
        [Browsable(false)]
        public string LanguageId { get; set; }

        [XmlIgnore]
        [Browsable(false)]
        public bool AddParagraph { get; set; }

        [DescriptionAttribute("Defines Margins of the Pdf document. (floating point)")]
        [Browsable(true)]
        public BookMargins Margins { get; set; }

        [Category("Texts")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Editor(typeof(InternationalStringEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(InternationalStringTypeConverter))]
        [DescriptionAttribute("Defines Cover title. ")]
        [DisplayName("Cover title")]
        public InternationalizedString CoverTitle { get; set; }

        [Category("Texts")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Editor(typeof(InternationalStringEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(InternationalStringTypeConverter))]
        [DescriptionAttribute("Defines Survey domain. ")]
        [DisplayName("Cover Sub title")]
        public InternationalizedString CoverSubTitle { get; set; }

        [Category("Texts")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Editor(typeof(InternationalStringEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(InternationalStringTypeConverter))]
        [DescriptionAttribute("Defines Title used in list of codelists module of CodeBook. ")]
        [DisplayName("Title Codelists Module")]
        public InternationalizedString TitleModuleCodelist { get; set; }

        [Category("Texts")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Editor(typeof(InternationalStringEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(InternationalStringTypeConverter))]
        [DescriptionAttribute("Defines Title used for variables detail. ")]
        [DisplayName("Title Variables detail")]
        public InternationalizedString TitleVarDetail { get; set; }

        [Category("Texts")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Editor(typeof(InternationalStringEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(InternationalStringTypeConverter))]
        [DescriptionAttribute("Defines Table of content title for CodeBook. ")]
        [DisplayName("Table of content title")]
        public InternationalizedString TableOfContentName { get; set; }

        [Category("Codelist")]
        [DescriptionAttribute("Integrate codelists in book")]
        [Browsable(true)]
        [DefaultValue(true)]
        public bool ShowCodelist { get; set; }

        [Category("Codelist")]
        [DescriptionAttribute("Integrate codelists resumed list in book")]
        [Browsable(true)]
        [DefaultValue(true)]
        [DisplayName("Codelist Resume")] 
        public bool ShowCodelistResume { get; set; }

        [Category("Codelist")]
        [DescriptionAttribute("Integrate codelists full detail in book" + 
            "\nNote: Only codelist having at least one code detail filled will be generated")]
        [Browsable(true)]
        [DefaultValue(false)]
        [DisplayName("Codelist Detail")]
        public bool ShowCodelistDetail { get; set; }

        [Category("Viewer")]
        [DescriptionAttribute("Uses external Pdf viewer or internal one" +
            "\nNote: Only codelist having at least one code detail filled will be generated")]
        [Browsable(true)]
        [DefaultValue(false)]
        [DisplayName("External Pdf viewer")]
        public bool ExternalPdfViewer { get; set; }

        [Category("Annotation")]
        [DescriptionAttribute("Provide a list of annotation Type to ignore")]
        [Browsable(true)]
        [DisplayName("Type ignore")]
        [Editor(@"System.Windows.Forms.Design.StringCollectionEditor," +
            "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
           typeof(System.Drawing.Design.UITypeEditor))]
        public List<string> AnnotIgnoreList { get; set; }

        [Category("Annotation")]
        [DescriptionAttribute("Provide a list of annotation Type to replace")]
        [Browsable(true)]
        [DisplayName("Type Replace")]
        [Editor(typeof(AnnotationItemRenameEditor), typeof(UITypeEditor))]
        public SerializableDictionary<string, InternationalizedString> AnnotReplaceType { get; set; }

        #endregion


        public string GetTranslatedAnnotationType(string type, string langCd)
        {
            return (AnnotReplaceType.ContainsKey(type)) ? AnnotReplaceType[type][langCd] : type;
        }

        /// <summary>
        /// Get logo of cover page according to selected language
        /// </summary>
        public byte[] GetLogo(string langCd)
        {
            byte[] logo = MyImageBytesEN; // default to englisk
            switch (langCd)
            {
                case "de": logo = MyImageBytesDE; break;
                case "fr": logo = MyImageBytesFR; break;
                case "it": logo = MyImageBytesIT; break;
            }
            return logo;
        }
    }
}
